//
//  RSearchViewController.m
//  Riji
//
//  Created by liujianlong on 2022/8/18.
//

#import "RSearchViewController.h"
#import "RNoteCell.h"
#import "RNoteInfoViewController.h"
@interface RSearchViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *input;
@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) UITapGestureRecognizer *tap;
@end

@implementation RSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [RLHud stopHud];
}
- (void)initUI{
    self.title = LS(@"搜索");
    [self.tableView registerNib:[RNoteCell nib] forCellReuseIdentifier:[RNoteCell ID]];
    for (NSLayoutConstraint *con in self.view.constraints) {
        if ([con.firstItem isEqual:self.tableView] && con.firstAttribute == NSLayoutAttributeTop) {
            con.constant = 50;
            break;
        }
    }
}
- (void)actionForTap
{
    if (self.input.isFirstResponder) {
        [self.view endEditing:YES];
        [self.view removeGestureRecognizer:self.tap];
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidChangeSelection:(UITextField *)textField
{
    if (textField.text.length <= 0) {
        self.data = [NSMutableArray array];
        [self.tableView reloadData];
        return;
    }
    if ([[self.view gestureRecognizers] count] <= 0) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionForTap)];
        [self.view addGestureRecognizer:self.tap];
    }
    
    NSString *s = textField.text;
    NSString *ID = [NSString stringWithFormat:@" %@=%@ ",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID)];
    NSString *remove = [NSString stringWithFormat:@" %@=%@ ",bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
    NSString *content = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"content"),s];
    NSString *address = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"address"),s];
    NSString *weather = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"weather"),s];
    NSString *feeling = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"feeling"),s];
    NSString *year = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"year"),s];
    NSString *month = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"month"),s];
    NSString *day = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"day"),s];
    NSString *week = [NSString stringWithFormat:@" %@ like '%%%@%%' ",bg_sqlKey(@"week"),s];
    NSString *time = [NSString stringWithFormat:@" %@ like '%%%@%%'",bg_sqlKey(@"time"),s];
    NSString* where = [NSString stringWithFormat:@"where%@and%@and%@or%@or%@or%@or%@or%@or%@or%@or%@",ID,remove
                       ,content,address,weather,feeling,year,month,day,week,time];
    [RLHud startHud:LS(@"搜索中...")];
    [RNoteModel bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [RLHud stopHud];
            if (array.count > 0) {
                self.data = [NSMutableArray arrayWithArray:array];
                [self.tableView reloadData];
            }
        });
    }];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RNoteCell *cell = [tableView dequeueReusableCellWithIdentifier:[RNoteCell ID]];
    cell.model = self.data[indexPath.row];
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    RNoteInfoViewController *VC = [RNoteInfoViewController new];
    VC.type = NotesTypeNormal;
    VC.index = (int)indexPath.row;
    VC.array = self.data;
    [self.navigationController pushViewController:VC animated:YES];
}

@end
